package com.powernode.crm.transaction.web;

import com.powernode.crm.activity.model.Activity;
import com.powernode.crm.activity.service.ActivityService;
import com.powernode.crm.commons.model.ChartVO;
import com.powernode.crm.commons.model.Constants;
import com.powernode.crm.commons.model.ResponseResult;
import com.powernode.crm.customer.model.Customer;
import com.powernode.crm.customer.service.CustomerService;
import com.powernode.crm.settings.service.DicService;
import com.powernode.crm.settings.service.UserService;
import com.powernode.crm.transaction.model.Tran;
import com.powernode.crm.transaction.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

@Controller
@RequestMapping("/transaction")
public class TransactionController {

    @Autowired
    private TranService tranService;

    @Autowired
    private UserService userService;
    @Autowired
    private DicService dicService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private CustomerService customerService;

       //    workbench/index
    @RequestMapping("/index")
    public String workBeanchIndex(){

        return "/workbench/transaction/index";
    }

    /*跳转 save*/
    @RequestMapping("/save")
    public String save(Model model){
        model.addAttribute("users",userService.queryAll());//所有者
        model.addAttribute("types",dicService.queryByTypeCode("transactionType"));//交易类型
        model.addAttribute("sources",dicService.queryByTypeCode("source"));//来源
        model.addAttribute("stages",dicService.queryByTypeCode("stage"));//阶段
        return "/workbench/transaction/save";
    }

    /*交易列表*/
    /*忽略模糊查询的 条件*/
    @RequestMapping("/api/tranList")
    @ResponseBody
    public ResponseResult tranList(Integer pageNum,Integer pageSize){
        ResponseResult result = new ResponseResult();

        pageNum=pageNum==null?1:pageNum;
        pageSize=pageSize==null?4:pageSize;

        /*查询列表*/
        List<Tran> tranList = tranService.queryPages(null,null,null,null,null,null,null,pageNum,pageSize);
        /*总行数*/
//        int rows = tranService.queryCount(null,null,null,null,null,null,null);
        /*忽略了页面数据*/
        Map<String,Object> data = new HashMap<>();
        data.put("tranList",tranList);
        data.put("rows",10);
        data.put("pageNum",pageNum);
        data.put("pages",4);
        result.setData(data);
        return result;
    }

    /*根据名称 模糊查询  市场活动列表*/
    @RequestMapping("/api/activityList")
    public @ResponseBody ResponseResult activityList(String name){
        ResponseResult result = new ResponseResult();
        List<Activity> activities = activityService.queryByName(name);
        result.setCode(Constants.STATUS_OK);
        result.setData(activities);
        return  result;
    }

    /*选择 可能性*/
    @RequestMapping("/api/possibility")
    @ResponseBody
    public ResponseResult possibility(String key){
        ResponseResult result = new ResponseResult();
        ResourceBundle bundle = ResourceBundle.getBundle("possibility");
        String value = bundle.getString(key);
        result.setCode(Constants.STATUS_OK);
        result.setData(value);
        return  result;
    }

    /*模糊查询 客户（消费者）名称*/
    @RequestMapping("/api/customerList")
    @ResponseBody
    public ResponseResult customerList(String name){
        ResponseResult result = new ResponseResult();
        int size = 10;
        List<Customer> customers = customerService.queryByName(name,size);
        result.setData(customers);
        return result;
    }

    /*交易详情*/

    @RequestMapping("/detail")
    public String detail(String id,Model model){
        /*通过id  获取  交易详情*/
        Tran tran = tranService.queryById(id);
        model.addAttribute("tran",tran);

        /*生产  阶段图标 */
        model.addAttribute("stages",dicService.queryByTypeCode("stage"));
        return "/workbench/transaction/detail";
    }

    @RequestMapping("/chart/index")
    public String charts(){

        return "/workbench/chart/transaction/index";
    }

    /*交易统计 列表*/
    @RequestMapping("/chart/api/chartVO")
    @ResponseBody
    public ResponseResult chartsVO(){
        ResponseResult result = new ResponseResult();

        List<ChartVO> vos = tranService.queryChartVO();
        result.setData(vos);

        return result;
    }
}
